package jUnitTest;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.junit.Test;

public class TestHomework0711 {

	@Test
	public void testRandom() {
		// 具体实现方法在myRandom中
		System.out.println("=============5的概率题=============");
		int sum = 0;
		for (int i = 0; i < 10000; i++) {
			if (myRandom() == 5) {
				sum++;
			}
		}
		double p = sum / 10000.0;
		System.out.println("5出现的概率：" + p);
	}

	@Test
	public void testSortForDog() {
		System.out.println("=============狗的排序题=============");
		List<Dog> dogs = new ArrayList<Dog>();
		for (int i = 0; i < 5; i++) {
			Dog dog = new Dog((int) (Math.random() * 10) + 1, (int) (Math.random() * 10) + 1, i);
			dogs.add(dog);
		}
		System.out.print("初始狗狗：");
		for (Dog d1 : dogs) {
			System.out.print("狗狗：" + d1.getName() + ": 体重：" + d1.getWeight() + ",年纪:" + d1.getAge() + "    ");
		}
		System.out.println();
		Collections.sort(dogs);
		System.out.print("体重排序：");
		for (Dog d1 : dogs) {
			System.out.print("狗狗：" + d1.getName() + ": 体重：" + d1.getWeight() + ",年纪:" + d1.getAge() + "    ");
		}
		System.out.println();
		Collections.sort(dogs, new DogCompareForAge());
		System.out.print("年龄排序：");
		for (Dog d1 : dogs) {
			System.out.print("狗狗：" + d1.getName() + ": 体重：" + d1.getWeight() + ",年纪:" + d1.getAge() + "    ");
		}
		System.out.println();
	}

	@Test
	public void testProperties() throws Exception {
		System.out.println("=============输出转化题=============");
		Properties properties = new Properties();
		Map<String, String> map = new HashMap<String, String>();
		properties.load(new FileInputStream("data.properties"));
		System.out.println("..........map键值对输出..........");
		for (Object o : properties.keySet()) {
			String string = o.toString();
			map.put(string, properties.getProperty(string).trim());
			System.out.println("<" + string + "," + map.get(string) + ">");
		}
		System.out.println("..........xml 输出..........");
		System.out.println("<User>");
		for (String s : map.keySet()) {
			String title = s + ">";
			String str = "<" + title + " " + map.get(s) + " </" + title;
			System.out.println(str);
		}
		System.out.println("</User>");
		System.out.println("..........json 输出..........");
		StringBuilder sb = new StringBuilder();
		sb.append("{");
		for (String s : map.keySet()) {
			sb.append(s + ":" + map.get(s) + ",");
		}
		sb.setLength(sb.length() - 1);
		sb.append("}");
		System.out.println(sb.toString());
	}

	public int myRandom() {
		int ran = (int) (Math.random() * 100) + 1;
		if (ran <= 30) {
			return 5;
		} else {
			ran = (int) (Math.random() * 9) + 1;
			if (ran < 5)
				return ran;
			else
				return ran + 1;
		}
	}

	public class Dog implements Comparable<Dog> {

		private int weight;
		private int age;
		private int name;

		public Dog(int weight, int age, int name) {
			super();
			this.weight = weight;
			this.age = age;
			this.name = name;
		}

		public int getName() {
			return name;
		}

		public void setName(int name) {
			this.name = name;
		}

		public int getAge() {
			return age;
		}

		public void setAge(int age) {
			this.age = age;
		}

		public int getWeight() {
			return weight;
		}

		public void setWeight(int weight) {
			this.weight = weight;
		}

		@Override
		public int compareTo(Dog o) {
			// TODO Auto-generated method stub
			return this.weight - o.weight;
		}

	}

	public class DogCompareForAge implements Comparator<Dog> {

		@Override
		public int compare(Dog o1, Dog o2) {
			// TODO Auto-generated method stub
			return o1.age - o2.age;
		}

	}

}
